package com.yyq.adms.usermanage.interceptor;

import javax.annotation.Resource;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

import com.yyq.adms.usermanage.bpo.SysOnlineUsersBpo;
import com.yyq.adms.usermanage.pojo.SysOnlineUsers;
import com.yyq.sos.constants.Constants;
import com.yyq.sos.permission.LoginUser;

public class UserSessionListener  implements   HttpSessionListener{
	private SysOnlineUsers onlineUsers;
	private SysOnlineUsersBpo OnlineBpo;
    
	@Resource(name="sysOnlineUsersBpo")
	public void setOnlineBpo(SysOnlineUsersBpo onlineBpo) {
		OnlineBpo = onlineBpo;
		OnlineBpo.setCls(onlineUsers.getClass());
	}
	
    public   void   sessionCreated(HttpSessionEvent   se){ 
    	//System.out.println("创建session*****************************"+se.getSession().getId());
    }   
    
    /*session失效时，删除在线用户表中用户信息*/   
    public   void   sessionDestroyed(HttpSessionEvent   se){  
    	//System.out.println("session失效了！*****************************"+se.getSession().getId());
    	LoginUser user = (LoginUser) se.getSession().getAttribute(Constants.LOGIN_USER);
		if(user!=null){
			onlineUsers=(SysOnlineUsers) OnlineBpo.findByPk(user.getUserId());
			if(onlineUsers!=null && user.getIp().equals(onlineUsers.getIp()))
				OnlineBpo.delete(user.getUserId());
			se.getSession().getServletContext().setAttribute(user.getIp(),user.getUserName());
			se.getSession().removeAttribute(Constants.LOGIN_USER);
		}else {
			//System.out.println("从session中取得LoginUser失败！****************************");
		}
    }   
}   

